<?php
function new_gf_inbox()
{
    include tiremanager_plugin_dir_path . "formsconfig.php";
    include tiremanager_plugin_dir_path . "sellers_brands.php";
	$offset = intval($_REQUEST['offset']);
	$row_id = intval($_REQUEST['rowid']);
	$count = intval($_REQUEST['count']);
	$page_size = 50;
	$paging=array('offset'=>$offset,'page_size'=>$page_size);
    $u = wp_get_current_user();
    if(!current_user_can('level_8')) {
	    $u->remove_cap( 'gravityforms_edit_entries' );
    }
    $u->add_cap('gravityforms_view_entries');
    $u = $u->user_login;
    $entries = array();
    $tire_admin_users = array();
    foreach ($sellers_array as $seller => $value) {
        if ($seller != $u) {
            array_push($tire_admin_users, $seller);
        }
    }

    $seller_users = array();
    foreach ($sellers_array as $seller_id => $value) {
        array_push($seller_users, $seller_id);
    }
    $entries = array();
	$first_time=$count==0;
	foreach ($tire_gf_fields_id_sellers_array as $sf => $value) {
        $search_criteria = array();
        $search_criteria['field_filters'][] = array('key' => '45', 'value' => "no");
        $search_criteria['field_filters'][] = array('key' => 'status', 'value' => 'active');
        if (in_array($u, $seller_users)) {
            $search_criteria['field_filters'][] = array('key' => "$sf", 'value' => "$u");
        }
        $entry = GFAPI::get_entries($tire_gravity_form_id, $search_criteria,null,$paging);
		if($first_time) {
			$count_entries = GFAPI::count_entries( $tire_gravity_form_id, $search_criteria,null,$paging );
			$count         = $count_entries > $count ? $count_entries : $count;
		}
        $entries = array_merge($entries, $entry);
    }
    $entry_rows = array();
    foreach ($entries as $e) {
        $id = $e['id'];
        $transaction_id = $e['transaction_id'];
        $first_name = $e['35'];
        $last_name = $e['36'];
        $quantity = $e['39'];
        $social_id = $e['27'];
        $size = "";
        $brand = "";
        $selected_seller = "";
        foreach ($tire_gf_fields_id_sellers_array as $seller_field => $value) {
            $seller_field = "$seller_field";
            $selected_seller = $selected_seller . $e[$seller_field];
        }
        if (!in_array($u, $seller_users)) {
            $u = $selected_seller;
        }
        foreach ($sellers_array[$u]['tire_type'] as $type) {

            foreach ($tire_gf_fields_id_tire_sizes[$type] as $item => $value) {
                $item = "$item";
                $size = $size . rgar($e, $item);
            }
        }
        foreach ($tire_gf_fields_id_brands_array as $b => $value) {
            $brand = $brand . rgar($e, "$b");

        }
        $entry_rows[] = array(
            'seller' => $selected_seller,
            'id' => $id,
            'transaction_id' => $transaction_id,
            'fname' => $first_name,
            'lname' => $last_name,
            'social_id' => $social_id,
            'tire_size' => $size,
            'quantity' => $quantity,
            'tire_brand' => $brand
        );

    }
    ?>
    <link rel="stylesheet" type="text/css"
          href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
	<?php if($count - $offset > $page_size){ ?>
    <a href=<?php echo esc_url( add_query_arg(
		array('offset'=> $offset + $page_size ,
		      'count'=>$count,
		      'rowid'=>sizeof($entries)+$row_id
		) ) );?>>
        <button class="button-primary"
                style="margin: 1ch;"
        >
            ادامه گزارش
        </button></a>
<?php } ?>
    <table id="gffields" class="display" style="width:100%">
        <thead>
        <tr>
            <th>ردیف</th>
            <th>کدملی</th>
            <?php if (current_user_can('level_1')) { ?>
                <th>عامل</th>
            <?php } else { ?>
                <th>کدرهگیری</th>
            <?php } ?>
            <th>برند</th>
            <th>سایز</th>
            <th>تعداد</th>
            <th>نام</th>
            <th>نام‌خانوادگی</th>
            <th>مشاهده جزییات</th>

        </tr>
        </thead>
        <tbody>
        <?php
        foreach ($entry_rows as $e) {
            echo "<tr>";
            echo "<td>" . ++$row_id . "</td>";
            echo "<td>" . $e['social_id'] . "</td>";
            if (current_user_can('level_1')) {
                echo "<td>" . $e['seller'] . "</td>";
            } else {
                echo "<td>" . $e['transaction_id'] . "</td>";
            }
            echo "<td>" . $e['tire_brand'] . "</td>";
            echo "<td>" . $e['tire_size'] . "</td>";
            echo "<td>" . $e['quantity'] . "</td>";
            echo "<td>" . $e['fname'] . "</td>";
            echo "<td>" . $e['lname'] . "</td>";
            echo "<td><a href=\"" . get_admin_url() . "
admin.php?page=gf_entries&view=entry&id="
                . $tire_gravity_form_id .
                "&lid=" . $e['id'] .
                "&order=ASC&filter&paged=1&pos=0&field_id&operator\">مشاهده</a></td></tr>";
        }
        ?>
        </tbody>
        <tfoot>
        <tr>
            <th>ردیف</th>
            <th>کدملی</th>
            <?php if (current_user_can('level_1')) { ?>
                <th>عامل</th>
            <?php } else { ?>
                <th>کدرهگیری</th>
            <?php } ?>
            <th>برند</th>
            <th>سایز</th>
            <th>تعداد</th>
            <th>نام</th>
            <th>نام‌خانوادگی</th>
            <th>مشاهده جزییات</th>

        </tr>
        </tfoot>
    </table>
	<?php if($count - $offset > $page_size){ ?>
    <a href=<?php echo esc_url( add_query_arg(
		array('offset'=> $offset + $page_size ,
		      'count'=>$count,
		      'rowid'=>$row_id
		) ) );?>>
        <button class="button-primary"
                style="margin: 1ch;"
        >
            ادامه گزارش
        </button></a>
<?php } ?>
    <script>
        $(document).ready(function () {
                $('#gffields').DataTable({
                    "pageLength": 100,
                    "pageLength": -1,
                    "paging":false,
                    "dom":"f",
                        "language": {
                            "search": "جست و جو",
                            "Show": "نمایش",
                            "paginate": {
                                "first": "اول",
                                "last": "آخر",
                                "next": "بعدی",
                                "previous": "قبلی"
                            },
                            "info": "صفحه _PAGE_ از _PAGES_",
                            "lengthMenu": "نمایش _MENU_ مورد",

                        },

                    }
                )
            }
        );
    </script>


    <?php


}

function read_gf_inbox()
{
    include tiremanager_plugin_dir_path . "formsconfig.php";
    include tiremanager_plugin_dir_path . "sellers_brands.php";
    $u = wp_get_current_user();
    $u->add_cap('gravityforms_edit_entries');
    $u->add_cap('gravityforms_view_entries');
    $u = $u->user_login;
    $entries = array();
    $tire_admin_users = array();
    $row_id = 0;
    foreach ($sellers_array as $seller => $value) {
        if ($seller != $u) {
            array_push($tire_admin_users, $seller);
        }
    }
    $seller_users = array();
    foreach ($sellers_array as $seller_id => $value) {
        array_push($seller_users, $seller_id);
    }
    $entries = array();
    foreach ($tire_gf_fields_id_sellers_array as $sf => $value) {
        $search_criteria = array();
        $search_criteria['field_filters'][] = array('key' => 'is_read', 'value' => true);
        $search_criteria['field_filters'][] = array('key' => 'status', 'value' => 'active');
        if (in_array($u, $seller_users)) {

            $search_criteria['field_filters'][] = array('key' => "$sf", 'value' => "$u");
        }
        $entry = GFAPI::get_entries($tire_gravity_form_id, $search_criteria);
        $entries = array_merge($entries, $entry);
    }
    $entry_rows = array();
    foreach ($entries as $e) {
        $id = $e['id'];
        $transaction_id = $e['transaction_id'];
        $first_name = $e['35'];
        $last_name = $e['36'];
        $social_id = $e['27'];
        $size = "";
        $brand = "";
        $selected_seller = "";
        foreach ($tire_gf_fields_id_sellers_array as $seller_field => $value) {
            $seller_field = "$seller_field";
            $selected_seller = $selected_seller . rgar($e, $seller_field);
        }
        if (!in_array($u, $seller_users)) {
            $u = $selected_seller;
        }
        foreach ($sellers_array[$u]['tire_type'] as $type) {

            foreach ($tire_gf_fields_id_tire_sizes[$type] as $item => $value) {
                $item = "$item";
                $size = $size . rgar($e, $item);
            }
        }
        foreach ($tire_gf_fields_id_brands_array as $b => $value) {
            $brand = $brand . rgar($e, "$b");

        }
        $entry_rows[] = array(
            'seller' => $u,

            'id' => $id,
            'transaction_id' => $transaction_id,
            'fname' => $first_name,
            'lname' => $last_name,
            'social_id' => $social_id,
            'tire_size' => $size,
            'tire_brand' => $brand
        );

    }
    ?>
    <link rel="stylesheet" type="text/css"
          href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

    <table id="gffields" class="display" style="width:100%">
        <thead>
        <tr>
            <th>ردیف</th>
            <th>کدملی</th>
            <?php if (current_user_can('level_1')) { ?>
                <th>عامل</th>
            <?php } else { ?>
                <th>کدرهگیری</th>
            <?php } ?>
            <th>برند</th>
            <th>سایز</th>
            <th>نام</th>
            <th>نام‌خانوادگی</th>
            <th>مشاهده جزییات</th>

        </tr>
        </thead>
        <tbody>
        <?php
        foreach ($entry_rows as $e) {
            echo "<tr>";
            echo "<td>" . ++$row_id . "</td>";
            echo "<td>" . $e['social_id'] . "</td>";
            if (current_user_can('level_1')) {
                echo "<td>" . $e['seller'] . "</td>";
            } else {
                echo "<td>" . $e['transaction_id'] . "</td>";
            }
            echo "<td>" . $e['tire_brand'] . "</td>";
            echo "<td>" . $e['tire_size'] . "</td>";
            echo "<td>" . $e['fname'] . "</td>";
            echo "<td>" . $e['lname'] . "</td>";
            echo "<td><a href=\"" . get_admin_url() . "
admin.php?page=gf_entries&view=entry&id="
                . $tire_gravity_form_id .
                "&lid=" . $e['id'] .
                "&order=ASC&filter&paged=1&pos=0&field_id&operator\">مشاهده</a></td></tr>";
        }
        ?>
        </tbody>
        <tfoot>
        <tr>
            <th>ردیف</th>
            <th>کدملی</th>
            <?php if (current_user_can('level_1')) { ?>
                <th>عامل</th>
            <?php } else { ?>
                <th>کدرهگیری</th>
            <?php } ?>
            <th>برند</th>
            <th>سایز</th>
            <th>نام</th>
            <th>نام‌خانوادگی</th>
            <th>مشاهده جزییات</th>

        </tr>
        </tfoot>
    </table>
    <script>
        $(document).ready(function () {
                $('#gffields').DataTable({
                        "pageLength": 100,
                        "language": {
                            "search": "جست و جو",
                            "Show": "نمایش",
                            "paginate": {
                                "first": "اول",
                                "last": "آخر",
                                "next": "بعدی",
                                "previous": "قبلی"
                            },
                            "info": "صفحه _PAGE_ از _PAGES_",
                            "lengthMenu": "نمایش _MENU_ مورد",

                        },

                    }
                )
            }
        );
    </script>


    <?php


}

function delivered_gf_inbox()
{
    include tiremanager_plugin_dir_path . "formsconfig.php";
    include tiremanager_plugin_dir_path . "sellers_brands.php";
	$offset = intval($_REQUEST['offset']);
	$row_id = intval($_REQUEST['rowid']);
	$count = intval($_REQUEST['count']);
	$page_size = 50;
	$paging=array('offset'=>$offset,'page_size'=>$page_size);
    $u = wp_get_current_user();
    $u->add_cap('gravityforms_edit_entries');
    $u->add_cap('gravityforms_view_entries');
    $u = $u->user_login;
    $entries = array();
    $tire_admin_users = array();
    $seller_users = array();
    foreach ($sellers_array as $seller_id => $value) {
        array_push($seller_users, $seller_id);
    }
    foreach ($sellers_array as $seller => $value) {
        if ($seller != $u) {
            array_push($tire_admin_users, $seller);
        }
    }
    $seller_users = array();
    foreach ($sellers_array as $seller_id => $value) {
        array_push($seller_users, $seller_id);
    }
    $entries = array();
	$first_time=$count==0;
	foreach ($tire_gf_fields_id_sellers_array as $sf => $value) {
        $search_criteria = array();
        $search_criteria['field_filters'][] = array('key' => '45', 'value' => 'yes');
        if (in_array($u, $seller_users)) {
            $search_criteria['field_filters'][] = array('key' => "$sf", 'value' => "$u");
        }
        $entry = GFAPI::get_entries($tire_gravity_form_id, $search_criteria);
		if($first_time) {
			$count_entries = GFAPI::count_entries( $tire_gravity_form_id, $search_criteria,null,$paging );
			$count         = $count_entries > $count ? $count_entries : $count;
		}
        $entries = array_merge($entries, $entry);
    }


    $entry_rows = array();
//    error_log(print_r($entries, true));
    foreach ($entries as $e) {
        $id = $e['id'];
        $transaction_id = $e['transaction_id'];
        $first_name = $e['35'];
        $last_name = $e['36'];
        $quantity = $e['39'];
        $social_id = $e['27'];
        $size = "";
        $brand = "";
        $selected_seller = "";
        foreach ($tire_gf_fields_id_sellers_array as $seller_field => $value) {
            $seller_field = "$seller_field";
            $selected_seller = $selected_seller . rgar($e, $seller_field);
        }
        if (!in_array($u, $seller_users)) {
            $u = $selected_seller;
        }
        foreach ($sellers_array[$u]['tire_type'] as $type) {

            foreach ($tire_gf_fields_id_tire_sizes[$type] as $item => $value) {
                $item = "$item";
                $size = $size . rgar($e, $item);
            }
        }
        foreach ($tire_gf_fields_id_brands_array as $b => $value) {
            $brand = $brand . rgar($e, "$b");

        }
        $entry_rows[] = array(
            'seller' => $u,
            'id' => $id,
            'transaction_id' => $transaction_id,
            'fname' => $first_name,
            'lname' => $last_name,
            'social_id' => $social_id,
            'tire_size' => $size,
            'quantity' => $quantity,
            'tire_brand' => $brand
        );

    }
    ?>
    <link rel="stylesheet" type="text/css"
          href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
	<?php if($count - $offset > $page_size){ ?>
    <a href=<?php echo esc_url( add_query_arg(
		array('offset'=> $offset + $page_size ,
		      'count'=>$count,
		      'rowid'=>sizeof($entries)+$row_id

		) ) );?>>
        <button class="button-primary"
                style="margin: 1ch;"
        >
            ادامه گزارش
        </button></a>
<?php } ?>
    <table id="gffields" class="display" style="width:100%">
        <thead>
        <tr>
            <th>ردیف</th>
            <th>کدملی</th>
            <?php if (current_user_can('level_1')) { ?>
                <th>عامل</th>
            <?php } else { ?>
                <th>کدرهگیری</th>
            <?php } ?>
            <th>برند</th>
            <th>سایز</th>
            <th>تعداد</th>
            <th>نام</th>
            <th>نام‌خانوادگی</th>
            <th>مشاهده جزییات</th>

        </tr>
        </thead>
        <tbody>
        <?php
        foreach ($entry_rows as $e) {
            echo "<tr>";
            echo "<td>" . ++$row_id . "</td>";
            echo "<td>" . $e['social_id'] . "</td>";
            if (current_user_can('level_1')) {
                echo "<td>" . $e['seller'] . "</td>";
            } else {
                echo "<td>" . $e['transaction_id'] . "</td>";
            }
            echo "<td>" . $e['tire_brand'] . "</td>";
            echo "<td>" . $e['tire_size'] . "</td>";
            echo "<td>" . $e['quantity'] . "</td>";
            echo "<td>" . $e['fname'] . "</td>";
            echo "<td>" . $e['lname'] . "</td>";
            echo "<td><a href=\"" . get_admin_url() . "
admin.php?page=gf_entries&view=entry&id="
                . $tire_gravity_form_id .
                "&lid=" . $e['id'] .
                "&order=ASC&filter&paged=1&pos=0&field_id&operator\">مشاهده</a></td></tr>";
        }
        ?>
        </tbody>
        <tfoot>
        <tr>
            <th>ردیف</th>
            <th>کدملی</th>
            <?php if (current_user_can('level_1')) { ?>
                <th>عامل</th>
            <?php } else { ?>
                <th>کدرهگیری</th>
            <?php } ?>
            <th>برند</th>
            <th>سایز</th>
            <th>تعداد</th>
            <th>نام</th>
            <th>نام‌خانوادگی</th>
            <th>مشاهده جزییات</th>

        </tr>
        </tfoot>
    </table>
	<?php if($count - $offset > $page_size){ ?>
    <a href=<?php echo esc_url( add_query_arg(
		array('offset'=> $offset + $page_size ,
		      'count'=>$count,
		      'rowid'=>$row_id
		) ) );?>>
        <button class="button-primary"
                style="margin: 1ch;"
        >
            ادامه گزارش
        </button></a>
<?php } ?>
    <script>
        $(document).ready(function () {
                $('#gffields').DataTable({
                        "pageLength": 100,
                    "pageLength": -1,
                    "paging":false,
                    "dom":"f",
                        "language": {
                            "search": "جست و جو",
                            "Show": "نمایش",
                            "paginate": {
                                "first": "اول",
                                "last": "آخر",
                                "next": "بعدی",
                                "previous": "قبلی"
                            },
                            "info": "صفحه _PAGE_ از _PAGES_",
                            "lengthMenu": "نمایش _MENU_ مورد",

                        },

                    }
                )
            }
        );
    </script>


    <?php


}
function all_registered_gf_inbox()
{
    include tiremanager_plugin_dir_path . "formsconfig.php";
    include tiremanager_plugin_dir_path . "sellers_brands.php";
    $offset = intval($_REQUEST['offset']);
    $row_id = intval($_REQUEST['rowid']);
    $count = intval($_REQUEST['count']);
    $page_size = 50;
    $paging=array('offset'=>$offset,'page_size'=>$page_size);
    $u = wp_get_current_user();
    $u->add_cap('gravityforms_edit_entries');
    $u->add_cap('gravityforms_view_entries');
    $u = $u->user_login;
    $entries = array();
    $tire_admin_users = array();
    $seller_users = array();
    foreach ($sellers_array as $seller_id => $value) {
        array_push($seller_users, $seller_id);
    }
    foreach ($sellers_array as $seller => $value) {
        if ($seller != $u) {
            array_push($tire_admin_users, $seller);
        }
    }
    $seller_users = array();
    foreach ($sellers_array as $seller_id => $value) {
        array_push($seller_users, $seller_id);
    }
    $entries = array();
    $first_time=$count==0;
    foreach ($tire_gf_fields_id_sellers_array as $sf => $value) {
        $search_criteria = array();
//        $search_criteria['field_filters'][] = array('key' => '45', 'value' => 'yes');
        if (in_array($u, $seller_users)) {
            $search_criteria['field_filters'][] = array('key' => "$sf", 'value' => "$u");
        }
        $entry = GFAPI::get_entries($tire_gravity_form_id, $search_criteria,null,$paging);
        if($first_time) {
	        $count_entries = GFAPI::count_entries( $tire_gravity_form_id, $search_criteria );
	        $count         = $count_entries > $count ? $count_entries : $count;
        }
        $entries = array_merge($entries, $entry);
    }


    $entry_rows = array();
//    error_log(print_r($entries, true));
    foreach ($entries as $e) {
        $id = $e['id'];
        $transaction_id = $e['transaction_id'];
        $first_name = $e['35'];
        $last_name = $e['36'];
        $quantity= $e['39'];
        $social_id = $e['27'];
        $delivered = strtolower($e['45'])==='yes'?'بله':'خیر';
        $size = "";
        $brand = "";
        $selected_seller = "";
        foreach ($tire_gf_fields_id_sellers_array as $seller_field => $value) {
            $seller_field = "$seller_field";
            $selected_seller = $selected_seller . rgar($e, $seller_field);
        }
        if (!in_array($u, $seller_users)) {
            $u = $selected_seller;
        }
        foreach ($sellers_array[$u]['tire_type'] as $type) {

            foreach ($tire_gf_fields_id_tire_sizes[$type] as $item => $value) {
                $item = "$item";
                $size = $size . rgar($e, $item);
            }
        }
        foreach ($tire_gf_fields_id_brands_array as $b => $value) {
            $brand = $brand . rgar($e, "$b");

        }
        $entry_rows[] = array(
            'seller' => $u,
            'id' => $id,
            'transaction_id' => $transaction_id,
            'fname' => $first_name,
            'lname' => $last_name,
            'social_id' => $social_id,
            'tire_size' => $size,
            'quantity'=>$quantity,
            'tire_brand' => $brands_array[$brand]['farsi_name'],
            'delivered'=>$delivered
        );

    }
    ?>
    <link rel="stylesheet" type="text/css"
          href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
	<?php if($count - $offset > $page_size){ ?>
    <a href=<?php echo esc_url( add_query_arg(
		array('offset'=> $offset + $page_size ,
		      'count'=>$count,
		      'rowid'=>sizeof($entries)+$row_id
		) ) );?>>
        <button class="button-primary"
                style="margin: 1ch;"
        >
            ادامه گزارش
        </button></a>
<?php } ?>
    <table id="gffields" class="display" style="width:100%">
        <thead>
        <tr>
            <th>ردیف</th>
            <th>کدملی</th>
            <th>تحویل شده</th>
            <?php if (current_user_can('level_1')) { ?>
                <th>عامل</th>
            <?php } else { ?>
                <th>کدرهگیری</th>
            <?php } ?>
            <th>برند</th>
            <th>سایز</th>
            <th>تعداد</th>
            <th>نام</th>
            <th>نام‌خانوادگی</th>
            <th>مشاهده جزییات</th>

        </tr>
        </thead>
        <tbody>
        <?php
        foreach ($entry_rows as $e) {
            echo "<tr>";
            echo "<td>" . ++$row_id . "</td>";
            echo "<td>" . $e['social_id'] . "</td>";
            echo "<td>" . $e['delivered'] . "</td>";
            if (current_user_can('level_1')) {
                echo "<td>" . $e['seller'] . "</td>";
            } else {
                echo "<td>" . $e['transaction_id'] . "</td>";
            }
            echo "<td>" . $e['tire_brand'] . "</td>";
            echo "<td>" . $e['tire_size'] . "</td>";
            echo "<td>" . $e['quantity'] . "</td>";
            echo "<td>" . $e['fname'] . "</td>";
            echo "<td>" . $e['lname'] . "</td>";
            echo "<td><a href=\"" . get_admin_url() . "
admin.php?page=gf_entries&view=entry&id="
                . $tire_gravity_form_id .
                "&lid=" . $e['id'] .
                "&order=ASC&filter&paged=1&pos=0&field_id&operator\">مشاهده</a></td></tr>";
        }
        ?>
        </tbody>
        <tfoot>
        <tr>
            <th>ردیف</th>
            <th>کدملی</th>
            <?php if (current_user_can('level_1')) { ?>
                <th>عامل</th>
            <?php } else { ?>
                <th>کدرهگیری</th>
            <?php } ?>
            <th>برند</th>
            <th>سایز</th>
            <th>تعداد</th>
            <th>نام</th>
            <th>نام‌خانوادگی</th>
            <th>مشاهده جزییات</th>

        </tr>
        </tfoot>
    </table>
	<?php if($count - $offset > $page_size){ ?>
    <a href=<?php echo esc_url( add_query_arg(
		array('offset'=> $offset + $page_size ,
		      'count'=>$count,
		      'rowid'=>$row_id
        ) ) );?>>
        <button class="button-primary"
                style="margin: 1ch;"
        >
            ادامه گزارش
        </button></a>
<?php } ?>
    <script>
        $(document).ready(function () {
                $('#gffields').DataTable({
                        "pageLength": -1,
                    "paging":false,
                    "dom":"f",
                        "language": {
                            "search": "جست و جو",
                            "Show": "نمایش",
                            "paginate": {
                                "first": "اول",
                                "last": "آخر",
                                "next": "بعدی",
                                "previous": "قبلی"
                            },
                            "info": "صفحه _PAGE_ از _PAGES_",
                            "lengthMenu": "نمایش _MENU_ مورد",

                        },

                    }
                )
            }
        );
    </script>


    <?php


}

function hide_field_from_edit()
{
    include tiremanager_plugin_dir_path . "formsconfig.php";
    if (!current_user_can('level_1')) {
        echo "
    <style> ";
        echo "table.form-table.entry-details tbody tr td.detail-view div h2.detail_gsection_title";
        $form = gfapi::get_form($tire_gravity_form_id);
        foreach ($form['fields'] as $field) {
            if (!in_array($field['id'], $delivered_fields)) {
                echo ",table.form-table.entry-details tbody tr td#field_5_" . $field['id'] . ".detail-view";
//            error_log($delivered_field);
            }
        }
        echo "{display:none;}";

//    foreach ()
        echo "</style>";
    }
}

function search_tires()
{

    include tiremanager_plugin_dir_path . "formsconfig.php";
    include tiremanager_plugin_dir_path . "sellers_brands.php";
    echo "<h5>لطفا اول با کد ملی و درصورت عدم نتیجه با کدرهگیری تلاش کنید</h5>";
    if (isset($_POST['search_social_id']) or
        isset($_POST['search_transaction_id'])) {
        $u = wp_get_current_user();
        $u = $u->user_login;
        $entries = array();
        $seller_users = array();
        $social_id = $_POST['search_social_id'];
        $transaction_id = $_POST['search_transaction_id'];

        foreach ($sellers_array as $seller => $value) {
            array_push($seller_users, $seller);
        }

        if (in_array($u, $seller_users)) {
            foreach ($tire_gf_fields_id_sellers_array as $sf => $value) {
                $search_criteria = array();
                if (strlen($social_id) > 8) {
                    $search_criteria['field_filters'][] = array('key' => '27',
                        'value' => "$social_id");
                } else {
                    $search_criteria['field_filters'][] = array('key' => 'transaction_id',
                        'value' => "$transaction_id");
                }
                $search_criteria['field_filters'][] = array('key' => 'status', 'value' => 'active');
                $search_criteria['field_filters'][] = array('key' => "$sf", 'value' => "$u");
                $entry = GFAPI::get_entries($tire_gravity_form_id, $search_criteria);
                $entries = array_merge($entries, $entry);
            }
        } else {
            if (strlen($_POST['search_social_id']) > 8) {
                $search_criteria['field_filters'][] = array('key' => '27',
                    'value' => "$social_id");
            } else {
                $search_criteria = array();
                $search_criteria['field_filters'][] = array('key' => 'transaction_id',
                    'value' => "$transaction_id");
            }
            $search_criteria['field_filters'][] = array('key' => 'status', 'value' => 'active');
            $entry = GFAPI::get_entries($tire_gravity_form_id, $search_criteria);
            $entries = array_merge($entries, $entry);
        }


        if (sizeof($entries) > 0) {
            if (sizeof($entries) > 1) {
                echo "
                <h1>
                کدملی بیشتر از یک بار ثبت‌نام کرده. لطفا مورد را گزارش دهید
                </h1>
                ";
            }
            foreach ($entries as $e) {
                $e_url = get_admin_url() . "
admin.php?page=gf_entries&view=entry&id="
                    . $tire_gravity_form_id .
                    "&lid=" . $e['id'] .
                    "&order=ASC&filter&paged=1&pos=0&field_id&operator";

                die("<h3>
            <a href=\"$e_url\">" . $e['35'] . " " . $e['36'] . "</a>
            </h3>");


            };
        }
    }



    ?>
    <form method="post" action="">
        <table>

            <tr>
                <td>
                    <h2>
                        براساس کد ملی
                    </h2>
                </td>
                <td>
                    <input name="search_social_id" type="text">
                </td>
                <td>

                </td>
            </tr>
            <tr>
                <td>
                    <h2>
                        براساس کد رهگیری
                    </h2>
                </td>
                <td>
                    <input name="search_transaction_id" type="text">
                </td>
                <td>

                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="جست و جو">
                </td>
            </tr>
        </table>


    </form>


    <?php
}

?>
